/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package system.ai;

import com.aionemu.gameserver.ai.AIClasss;
import com.aionemu.gameserver.ai.CreatureAI;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.instance.Servant;
import com.aionemu.gameserver.skillengine.SkillEngine;
import com.aionemu.gameserver.skillengine.model.Skill;
import com.aionemu.gameserver.utils.MathUtil;

/**
 * @author Mr. Poke
 *
 */
public class ServantAI extends CreatureAI
{

	/**
	 * @param owner
	 */
	public ServantAI(Creature owner)
	{
		super(owner);
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.ai.CreatureAI#getOwner()
	 */
	@Override
	public Servant getOwner()
	{
		return (Servant) super.getOwner();
	}

	@Override
	protected void onEvtDead()
	{
		setTarget(null);
		stopAITask();
		super.onEvtDead();
	}

	@Override
	public final synchronized void setAttackTarget(Creature target)
	{
		intentionArg0 = target;
	}

	@Override
	public final Creature getAttackTarget()
	{
		return (Creature) intentionArg0;
	}

	/**
	 * Manage AI thinking actions of a L2Attackable.<BR><BR>
	 */
	@Override
	protected void onEvtThink()
	{
		// Check if the actor can't use skills and if a thinking action isn't already in progress
		if (getOwner().isAllSkillsDisabled())
			return;
		Creature enemy = (Creature) getOwner().getTarget();
		if (getOwner().getMaster() == null || enemy == null || enemy.getLifeStats().isAlreadyDead())
		{
			getOwner().onDespawn();
			return;
		}
		if (getOwner().isEnemy(enemy))
		{

			if (getOwner().getSkillId() == 0)
			{
				float attackRange = getOwner().getBoundingRadius().getCollision() + enemy.getBoundingRadius().getCollision()
						+ (getOwner().getGameStats().getAttackRange().getCurrent() / 1000f);
				if (MathUtil.getDistance(owner, enemy) > attackRange)
					return;
				attackTarget();
			}
			else
			{
				Skill skill = SkillEngine.getInstance().getSkill(getOwner(), getOwner().getSkillId(), 1, target);
				if (skill != null)
				{
					skill.useSkill();
				}
			}
		}
		getOwner().getLifeStats().reduceHp((getOwner().getHpRatio() / 100) * getOwner().getLifeStats().getMaxHp(), null);
	}

	public static void main(String[] args)
	{
		AIClasss.getInstance().addClass("ServantAI", ServantAI.class);
	}
}
